package com.limao.CacheManager;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.MobileCodeDataClass;
import com.limao.DBProvider.MobileCodeProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class mobile_codeCacheManger extends BasicModule{

	private MobileCodeProvider provider;
	private static mobile_codeCacheManger instance;
	
	private Cache<String, MobileCodeDataClass> mobileCodeCacheManger;
	private Cache<String, MobileCodeDataClass> regNumCacheManger;
	private Cache<String, MobileCodeDataClass> numCacheManger;

    public static mobile_codeCacheManger getInstance() {
        return instance;
    }

    public mobile_codeCacheManger() {
    	super("mobileCodeCacheManger");
    	mobileCodeCacheManger = CacheFactory.createCache("cd_mobileCodeCache");
    	regNumCacheManger= CacheFactory.createCache("cd_regNumCacheManger");
    	numCacheManger= CacheFactory.createCache("cd_NumCacheManger");
    	
		initProvider();
    }

    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    
    
    
    public void addNumber(String ipStr)
    {
    	
    	MobileCodeDataClass obj=numCacheManger.get(ipStr);
		if(obj==null)
    	{
    		obj=new MobileCodeDataClass();
    		obj.ip=ipStr;
    		obj.sendnum="1";
    		obj.loadcodetime=CacheClass.getTime()+"";
    		numCacheManger.put(ipStr, obj);
    	}
    	else
    	{
   			obj.sendnum=(CacheClass.getIntValue(obj.sendnum)+1)+"";
   			numCacheManger.put(ipStr, obj);
    	}
    }
    
   
    public int getNumber(String ipStr)
    {
    	return getNumber(ipStr,0);
    }
    public int getNumber(String ipStr,int type)
    {
    	
    	int num=0;
    	switch(type)
    	{
     	case 0:
     		MobileCodeDataClass obj=mobileCodeCacheManger.get(ipStr);
    		if(obj==null)
        	{
        		obj=new MobileCodeDataClass();
        		obj.ip=ipStr;
        		obj.sendnum="1";
        		obj.loadcodetime=CacheClass.getTime()+"";
        		mobileCodeCacheManger.put(ipStr, obj);
        	}
        	else
        	{
        		int minute=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("mobilecode_ip_minute").trim());
        		if(CacheClass.getTime()-CacheClass.getLongValue(obj.loadcodetime)>minute*60*1000)//大于1分钟
        		{
            		obj.sendnum="1";
            		obj.loadcodetime=CacheClass.getTime()+"";
            		mobileCodeCacheManger.put(ipStr, obj);
        		}
        		else  //1分钟内
        		{
        			obj.sendnum=(CacheClass.getIntValue(obj.sendnum)+1)+"";
        			mobileCodeCacheManger.put(ipStr, obj);
        		}
        		
        	}
    		num=CacheClass.getIntValue(obj.sendnum);
    		break;
    	case 1:
    		MobileCodeDataClass tmp=regNumCacheManger.get(ipStr);
        	if(tmp==null)
        	{
        		tmp=new MobileCodeDataClass();
        		tmp.ip=ipStr;
        		tmp.regnum="1";
        		tmp.regtime=CacheClass.getTime()+"";
        		regNumCacheManger.put(ipStr, tmp);
        	}
        	else
        	{
        		int minute=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("reg_ip_minute").trim());
        		if(CacheClass.getTime()-CacheClass.getLongValue(tmp.regtime)>minute*60*1000)//大于1分钟
        		{
        			tmp.regnum="1";
        			tmp.regtime=CacheClass.getTime()+"";
            		regNumCacheManger.put(ipStr, tmp);
        		}
        		else  //1分钟内
        		{
        			tmp.regnum=(CacheClass.getIntValue(tmp.regnum)+1)+"";
        			regNumCacheManger.put(ipStr, tmp);
        		}
        		
        	}
    		break;
    	}
    	return num;
    	
    	
    }

    
    
    public MobileCodeDataClass createCode(String mobile,String code)
    {
    	MobileCodeDataClass obj=provider.createCode(mobile, code);

    	return obj;
    }

    public MobileCodeDataClass loadMobileCode(String mobile) {
       	if (mobile == null) {
            return null;
        }
       	provider.deleteExpiredCode();//先删除数据库中已过期的纪录
        
        return provider.Load_Mobile_Code(mobile);
    }

    //更新本次取验证码时间
    public MobileCodeDataClass updateCodeTime(String mobile, String code, long loadcodetime)
    {
    	MobileCodeDataClass obj=provider.updateCodeTime(mobile, code, loadcodetime);

    	return obj;
    }
    
    public boolean deleteMobileCode(String mobile)//删除指定的某条
    {
    	boolean tmp=false;
    	tmp=provider.deleteMobileCode(mobile);
    	return tmp;
    }
    
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =MobileCodeProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}
